<?php

namespace app\api\controller\order;


use addons\epay\library\Service;
use app\admin\model\Conference;
use app\common\controller\Api;
use app\common\library\Iden;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Response;
use think\Session;
use app\admin\model\vactivities\Ticket;
use Endroid\QrCode\QrCode;
use Yansongda\Pay\Pay;
use think\Log;

// 允许跨域
header('Access-Control-Allow-Origin:*');//允许跨域

/**
 * 个人中心
 *加入活动
 */
class Join extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $model = null;
    protected $catemodel = null;
    protected $tagsmodel = null;
    protected $actmodel = null;
    protected $ticket_model = null;
    protected $aact = null;
    protected $api_act = null;
    protected $mem_model = null;
    protected $Infomodel = null;
    protected $gainlogmodel = null;
    protected $gainmodel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\trades\assets\Order;//订单
        $this->Infomodel = new \app\admin\model\trades\assets\Orderinfo;//订单详情
        $this->gainlogmodel = new \app\admin\model\trades\order\Gainlog;//收益详情
        $this->gainmodel = new \app\admin\model\trades\order\Gain;//收益总表
        $this->actmodel = new \app\admin\model\vactivities\Activity;
        $this->catemodel = new \app\admin\model\superdata\Activitycategory;
        $this->tagsmodel = new \app\admin\model\superdata\Activitytags;
        $this->ticket_model = new \app\admin\model\vactivities\Ticket;
        $this->admin_model = new \app\admin\model\Admin;
        $this->mem_model = new \app\admin\model\member\Account;

    }



    /*
     * 报名活动信息
     */
    public function activity(){

        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error(__('Please login first'), null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        if(empty($admin_id)){
            $this->error(__('获取用户参数出错'));
        }

        $ids = $this->request->param("ids");//活动id
        if (!$ids) {
            $this->error(__('获取参数错误'),'',0);
        }
        $ids = Iden::decrypt($ids);

        $row = $this->actmodel->get(['id' => $ids]);
        if (!$row) {
            $this->error('没有找到结果');
        }
        $row = $row->toArray();//活动信息


        if($row['status']!= $this->actmodel::ACTIVITY_STATUS_1){
            $this->error('状态有误，此状态暂时未上线');
        }

        $nowTime = time();
        $stime = $row['startTime'];
        $isticket = $row['isticket'];
        $duration = $row['duration'];

        $tArr = [$this->actmodel::TICKET_STATUS_0,$this->actmodel::TICKET_STATUS_1,$this->actmodel::TICKET_STATUS_2];
        if(!in_array($isticket,$tArr)){
            $this->error('票务类型错误');
        }

        switch ($isticket)
        {
            case 0://报名
                $this->Isenroll($ids);
                break;
            case 1://购买
                $this->Isbuy($ids);
                break;
            case 2://直接加入活动
                $this->directjoin($ids);
                break;
            default:
                return false;
                break;
        }

         $time_diff = $stime - $nowTime;//时间差
         if($time_diff > 0 && $time_diff > 60 * 5){//活动前5分钟才能加入
             $this->error('活动前5分钟才能加入活动', null, 0);
         }

        if($stime + $duration < $nowTime){//活动结束时间
            $this->error(('此活动已结束,不能再加入活动'), null, 0);
        }


        $sessionId = session_id();

        $confModel = new Conference();
        $userToken = $confModel->getUserToken($admin_id, $uInfo['token']);

        $displayName = $uInfo['nickname'];
        $peerName = $uInfo['serial'];

        $roomUrl = config('join_room_url') . "?m={$row['serial']}&peerName={$peerName}&displayName={$displayName}&PHPSESSID={$sessionId}&token={$userToken}";

        if(empty($roomUrl))
            $roomUrl = '';

        $this->success(__('加入成功'), ['url'=>$roomUrl], 200);
    }



    /*
     * $ids 活动 id
     *  判断是否报名
     */
    private function Isenroll($ids){

        if(empty($ids)){
            $this->error(__('获取参数错误'), null, 0);
        }
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error('您还没登发,请先登录后再操作', null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        if(empty($admin_id)){
            $this->error(__('获取用户参数出错'));
        }

        $r =  Db::name('activity_order')
            ->where('buyer_id', $admin_id)
            ->where('activity_id', $ids)
            ->where('ticket_type', $this->actmodel::TICKET_STATUS_0)
            ->find();

        if(!$r){
            $this->error('您还没报名,请报名再加入活动');
        }

        return true;
    }

    /*
        * $ids 活动 id
        *  判断是否购买
        */
    private function Isbuy($ids){

        if(empty($ids)){
            $this->error(__('获取参数错误'), null, 0);
        }
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error('您还没登发,请先登录后再操作', null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        if(empty($admin_id)){
            $this->error(__('获取用户参数出错'));
        }

        $r =  Db::name('activity_order')
            ->where('buyer_id', $admin_id)
            ->where('activity_id', $ids)
            ->where('ticket_type', $this->actmodel::TICKET_STATUS_1)
            ->where('state', $this->model::PAY_STATE_2)
            ->find();

        if(!$r){
            $this->error(('您还没购买优惠卷,请购买后加入活动'), null, 0);
        }

        return true;
    }


    /*
     *  $ids 活动 id
     *  直接加入活动
     */
    private function directjoin($ids){

        if(empty($ids)){
            $this->error(__('获取参数错误'), null, 0);
        }
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error('您还没登录,请先登录', null, 401);
        }


        return true;
    }

}
